package com.hxy.org.service.impl;

import com.hxy.basic.query.util.MD5Utils;
import com.hxy.basic.query.util.PageList;
import com.hxy.basic.query.util.StrUtils;
import com.hxy.basic.service.impl.BaseServiceImpl;
import com.hxy.org.domain.Department;
import com.hxy.org.domain.Employee;
import com.hxy.org.mapper.EmployeeMapper;
import com.hxy.org.query.EmployeeQuery;
import com.hxy.org.service.IEmployeeService;
import com.hxy.user.domain.LoginInfo;
import com.hxy.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 *
 * 员工接口实现类
 * @authorr  Hxy
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void save(Employee employee) {

    }

    private LoginInfo employee2LoginInfo(Employee employee) {
        LoginInfo info = new LoginInfo();
        BeanUtils.copyProperties(employee, info);
        info.setType(0);//0表示员工
        return info;
    }


    @Override
    @Transactional
    public void delete(Long employee_id) {
        // t通过id获取管理员对象
        Employee employee = employeeMapper.loadById(employee_id);
        Long logininfo_id = employee.getLogininfo_id();
        // 根据logininfo—_di删除Lgininfo对象
        loginInfoMapper.delete(logininfo_id);
        // 删除Employee对象
        super.delete(employee_id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String newMd5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(newMd5Pwd);
        // 将employee转成logininfo对象
        LoginInfo loginInfo = employee2LoginInfo(employee);
        //修改操作-根据id修改
        Employee dbEmployee = employeeMapper.loadById(employee.getId());
        loginInfo.setId(dbEmployee.getLogininfo_id());
        loginInfoMapper.update(loginInfo);

        super.update(employee);
    }
}

















